package com.pioneer.cloudstorage.controller;

import com.pioneer.cloudstorage.pojo.dto.SharesDTO;
import com.pioneer.cloudstorage.pojo.entity.Result;
import com.pioneer.cloudstorage.pojo.entity.ShareId;
import com.pioneer.cloudstorage.service.SharesService;
import com.pioneer.cloudstorage.utils.BaseContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/api/shares")
@Slf4j
public class Shares {
    @Autowired
    SharesService sharesService;
    /**
     * 分享文件,创建链接
     * @param sharesDTO
     * @return
     */
    @PostMapping
    public Result share(@RequestBody SharesDTO sharesDTO) throws Exception {
        String id = sharesService.share(sharesDTO);
        ShareId shareId = new ShareId();
        shareId.setId(id);
        log.info(String.valueOf(sharesDTO.getExpireTime()));
        log.info("创建分享链接成功，shareId: {}", id);
        return Result.success(shareId);
    }
    /**
     * 获取分享列表
     * @return
     */
    @GetMapping
    public Result getShareList() throws Exception {
        return Result.success(sharesService.getShareList());
    }

    /**
     * 删除分享链接
     * @param shareId
     * @return
     */
    @DeleteMapping("/{shareId}")
    public Result deleteShare(@PathVariable String shareId) {
        return sharesService.deleteShare(shareId);
    }
    /**
     * 获取分享链接
     * @param shareId
     * @param password
     * @return
     */
    @GetMapping("/{shareId}")
    public Result getShare(@PathVariable String shareId,
                           @RequestParam(required = false) String  password) throws Exception {
        return sharesService.getShare(shareId, password);
    }
    /**
     * 下载分享文件
     * @param shareId
     * @param password
     * @param response
     */
    @GetMapping("/{shareId}/download")
    public void downloadShare(@PathVariable String shareId,
                                @RequestParam(required = false)  String password,
                                HttpServletResponse response) throws Exception {
        log.info("用户访问分享Id: {}", shareId);
         sharesService.downloadShare(shareId, password, response);
    }

}
